//获取应用实例
const app = getApp()

Page({
    data: {
      showConfirm:false,
      boxId:"",
      report:{},
      reportId:"",
      score_item:'',
      image_file_urls:[],
      resourceIndex:0,
      tempFilePath:"",
      loading:false,
      isShowSubmit:false
    },
    handleSuccess:function(e){
      const { files } = e.detail;
      console.log('files',files)
      // this.setData({
      //   originFiles: files,
      // });
    },
    handleClick(e) {
      console.log(e.detail.file);
    },

    onLoad: function(options) {
      if (options != null){
        const boxId= options.boxId;
        const score_item= options.score_item;
        this.setData({boxId,score_item})
        var that = this;
        wx.request({
          url: app.globalData.url+'/wechat/student_report',
          data: {
            username: app.globalData.localuserinfo.username,
            boxId,
            score_item
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: res2 => {
            this.setData({
              report:res2.data||{},
              image_file_urls:res2.data&&res2.data.image_file_urls||[],
              reportId:res2.data&&res2.data.objectId
            });
            wx.setNavigationBarTitle({
              title: score_item
          })
          console.log("image_file_urls",res2.data)
          }
        })
        
      }
    },

    showDialog:function(e){
      const index=e.target.id
      this.setData({showConfirm:true,resourceIndex:index})
    },
    closeDialog:function(){
      this.setData({showConfirm:false})
    },

    onChange:function(e){
      console.log("onChange")
    },

    addImage:function(){
      const that=this;
      this.setData({loading:true})
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePath = res.tempFilePaths[0];
          that.setData({tempFilePath});
          console.log("photo path："+tempFilePath);
          that.upload(tempFilePath,'image')
        }
      })
    },

    upload:function(tempFilePath,type){
      const that=this;
      console.log('tempFilePath',tempFilePath)
      wx.request({
        url: app.globalData.url + '/wechat/qiniu_upload_token',
        method: 'get',
        success: res => {
          console.log(res.data);
          const uploadTask = wx.uploadFile({
            url: res.data.url,
            header: { 'content-type': 'multipart/form-data' },
            method: 'POST',
            filePath: tempFilePath,
            name: 'file',
            formData: {
              'token': res.data.upload_token
            },
            success: function (res2) {
              const data = JSON.parse(res2.data);
              const fileurl=res.data.qiniu_domain+data.key;
              const image_file_urls=that.data.image_file_urls;
              image_file_urls.push(fileurl);
             wx.request({
                url: app.globalData.url+'/wechat/update_report',
                data: {
                  id: that.data.reportId,
                  key:"image_file_urls",
                  value:image_file_urls,
                  token:app.globalData.token
            },
            method: 'POST',
             header: {
              'content-type': 'application/json'
             },
            success: res2 => {
              that.setData({
                report:res2.data||{},
                image_file_urls,
            });
          }
        })
            },
            fail: function (err) {
              console.log(err);
              wx.showToast({
                title: '上传失败，图片服务器出错：' + err.errMsg,
                icon: 'none',
                duration: 2000
              })
              that.setData({
                loading: false
              })
            },
          })
          uploadTask.onProgressUpdate((res) => {
            if (res.progress == 100) {
              wx.showToast({
                title: '上传成功！',
                icon: 'success',
                duration: 2000
              })
              that.setData({
                loading: false
              })
            }
          })
        },
        fail: function (err) {
          console.log(err);
          wx.showToast({
            title: '上传失败，获取token出错：' + err,
            icon: 'none',
            duration: 2000
          })
          that.setData({
            loading: false
          })
        }
      });
    },

    removeResource:function(){
      const that=this;
      const image_file_urls=this.data.image_file_urls;
      image_file_urls.splice(this.data.resourceIndex,1);
      wx.request({
        url: app.globalData.url+'/wechat/update_report',
        data: {
          id: that.data.reportId,
          key:"image_file_urls",
          value:image_file_urls,
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          this.setData({
            report:res2.data||{},
            image_file_urls:image_file_urls,
            showConfirm:false
          });
        }
      })
    },
    upResource:function(e){
      const that=this;
      const idx=e.target.id;
      const image_file_urls=this.data.image_file_urls;
      if (idx <= 0) {
        return;
    }
    const oResource=image_file_urls[idx];
    image_file_urls.splice(idx, 1);//删除原id
    image_file_urls.splice(idx - 1, 0, oResource);
    wx.request({
      url: app.globalData.url+'/wechat/update_report',
      data: {
        id: that.data.reportId,
        key:"image_file_urls",
        value:image_file_urls,
        token:app.globalData.token
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res2 => {
        this.setData({
          report:res2.data||{},
          image_file_urls:image_file_urls,
          showConfirm:false
        });
      }
    })
    },
    downResource:function(e){
      const that=this;
      const idx=e.target.id;
      const image_file_urls=this.data.image_file_urls
        if (idx >= image_file_urls.length - 1) {
            return;
        }
        const oResource=image_file_urls[idx];
        image_file_urls.splice(idx, 1);//删除原id
        image_file_urls.splice(idx + 1, 0, oResource);
        wx.request({
          url: app.globalData.url+'/wechat/update_report',
          data: {
            id: that.data.reportId,
            key:"image_file_urls",
            value:image_file_urls,
            token:app.globalData.token
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: res2 => {
            this.setData({
              report:res2.data||{},
              image_file_urls:image_file_urls,
              showConfirm:false
            });
          }
        })
    },

    closeSubmit:function(){
      this.setData({isShowSubmit:false})
    },
    showSubmit:function(){
      this.setData({isShowSubmit:true})
    },
    submitReport:function(){
      const that=this;
      wx.request({
        url: app.globalData.url+'/wechat/update_report',
        data: {
          id: that.data.reportId,
          key:"status",
          value:'已提交/未批改',
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          wx.navigateTo({
            url: "../student_report_box/report_box?boxId="+that.data.boxId,
        });
        }
      })
    }
  
})
